﻿using System;
using System.Text;
using ColorApplication.Constants;

namespace ColorApplication.BusinessLayer
{
    public class ReportObject
    {
        public int Id { get; set; }
        public DateTime AnalysisDate { get; set; }
        public string PictureName { get; set; }
        public decimal Size { get; set; }
        public string PaymentTransactionId { get; set; }
        public string PaymentDetails { get; set; }
        public bool PaymentStatus { get; set; }
        public AnalysisOption SelectedOption { get; set; }
        public string WantedFamilyLists { get; set; }
        public string AnalysisUrl { get; set; }

        public string SelectedOptionLiteral
        {
            get
            {
                switch (SelectedOption)
                {
                    case AnalysisOption.OnePicture:
                        return "One picture";
                        break;
                    case AnalysisOption.TwoPicture:
                        return "Two pictures";
                        break;
                    default:
                        return "from URL";
                        break;
                }
            }
        }

        public string SummaryResults { get; set; }
        public string MatchingAttrs { get; set; }

        public decimal MarksOfWantedFamilies { get; set; }

        /// <summary>
        /// number of how many expected attributes were chosen 
        /// </summary>
        public int NumberOfMatching { get; set; }
        public int ExpectedAttrsCount { get; set; }
        public string PieFileName { get; set; }

        public decimal SumOfMatching
        {
            get;
            set;
        }

        public string NumberOfMatchingPerExpected
        {
            get { return GetNumberOfMatchingPerExpectedAndSetSummaryMatching(); }
        }

        private string GetNumberOfMatchingPerExpectedAndSetSummaryMatching()
        {
            var arrSummaryResults = SummaryResults.Split('|');
            var arrWantedAttr = WantedFamilyLists.Split(',');

            var hasWantedFamily = !string.IsNullOrEmpty(WantedFamilyLists);

            var matching = 0;
            decimal sumOfMatching = 0;

            if (hasWantedFamily)
            {
                foreach (var arrSummaryResult in arrSummaryResults)
                {
                    var subArr = arrSummaryResult.Split('-');
                    if (subArr.Length != 2) continue;

                    var attName = subArr[0].Trim();

                    foreach (var wantedAttr in arrWantedAttr)
                    {
                        var wAttName = wantedAttr.Split('-')[0].Trim();
                        if (string.Compare(wAttName, attName, StringComparison.OrdinalIgnoreCase) == 0)
                        {
                            matching += 1;
                            var subSum = RemoveSpecialCharacters(subArr[1].Trim());
                            sumOfMatching += decimal.Parse(subSum);
                            break;
                        }
                    }
                }
            }

            SumOfMatching = sumOfMatching;
            NumberOfMatching = matching;
            NumberOfExpected = hasWantedFamily ? arrWantedAttr.Length : 0;

            return string.Format("{0}/{1}", matching, NumberOfExpected);
        }

        public int NumberOfExpected { get; set; }

        public static string RemoveSpecialCharacters(string str)
        {
            var sb = new StringBuilder();
            foreach (char c in str)
            {
                if ((c >= '0' && c <= '9') || c == '.')
                {
                    sb.Append(c);
                }
            }
            return sb.ToString();
        }
    }
}